$(function () {
    findAll();
})

function findAll() {
    $.ajax({
        url: '/ProductFindAll',
        data: '',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            setData(data[0].object);
        }
    })
}

function setData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<tr>\n' +
            '        <td><input type="checkbox"></td>\n' +
            '        <td>' + data[i].id + '</td>\n' +
            '        <td><img src="' + data[i].imgHref + '" width="50px" href="50px"></td>\n' +
            '        <td>' + data[i].title + '</td>\n' +
            '        <td>' + data[i].price + '</td>\n' +
            '        <td>' + data[i].content + '</td>\n' +
            '        <td>' + (data[i].createTime == null ? "" : data[i].createTime) + '</td>\n' +
            '        <td>' + data[i].name + '</td>\n' +
            '        <td><a href="javascript:edit(' + data[i].id + ')">编辑</a> ' +
            '<a href="javascript:del(' + data[i].id + ')">删除</a></td>\n' +
            '    </tr>';
    }
    $("#productFindAll").html(html);
}

function edit(id) {
    localStorage.setItem("productId", id);
    $('#right').load('/back/productAdd.jsp');
}

function del(id) {
    $.ajax({
        url: '/productDelById',
        type: 'post',
        data: {id: id},
        dataType: 'json',
        success: function (data) {
            console.log(data + "--------");
            if (data[0].object == 1) {
                $('#right').load('/back/productFindAll.jsp');
            } else {
                alert("删除失败");
            }

        }
    })
}